#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
#include<string>
using namespace std;
class Solution {
public:
    vector<int> findAnagrams(string s, string p)
    {
        vector<int>ret;
        int hash2[26] = { 0 };
        for (auto x : p)
        {
            hash2[x - 'a']++;
        }
        int len = p.size();
        int hash1[26] = { 0 };
        for (int left = 0, right = 0, count = 0; right < s.size(); right++)
        {
            char in = s[right];

            if (++hash1[in - 'a'] <= hash2[in - 'a'])count++;

            if (right - left + 1 > len)
            {
                int out = s[left++];
                if (hash1[out - 'a']-- <= hash2[out - 'a'])
                    count--;
            }
            if (count == len)
                ret.push_back(left);
        }

        return ret;
    }
};
int main()
{
    const char* s = "cbaebabacd"; 
    const char* p = "abc";

        Solution m;

        vector<int> n;
        n=m.findAnagrams(s, p);

        cout << "[";
        for (auto x : n)
        {
            cout  << x << " ";
        }
        cout << "]";

    return 0;
}